/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

package com.huawei.rsu.demo;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.dris.v1.DrisClient;
import com.huaweicloud.sdk.dris.v1.model.AddRsuDTO;
import com.huaweicloud.sdk.dris.v1.model.BatchShowRsusRequest;
import com.huaweicloud.sdk.dris.v1.model.BatchShowRsusResponse;
import com.huaweicloud.sdk.dris.v1.model.CreateRsuRequest;
import com.huaweicloud.sdk.dris.v1.model.CreateRsuResponse;
import com.huaweicloud.sdk.dris.v1.model.DeleteRsuRequest;
import com.huaweicloud.sdk.dris.v1.model.DeleteRsuResponse;
import com.huaweicloud.sdk.dris.v1.model.ModifyRsuRequestDTO;
import com.huaweicloud.sdk.dris.v1.model.UpdateRsuRequest;
import com.huaweicloud.sdk.dris.v1.model.UpdateRsuResponse;
import com.huaweicloud.sdk.dris.v1.region.DrisRegion;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * RSU管理示例
 *
 * @author p00631112
 * @since 2023-01-05
 */
public class RsuSolutionDemo {
    private static final Logger logger = LoggerFactory.getLogger(RsuSolutionDemo.class);

    public static void main(String[] args) {
        String ak = "<YOUR AK>";
        String sk = "<YOUR SK>";

        // 创建DrisClient实例
        ICredential auth = new BasicCredentials()
            .withAk(ak)
            .withSk(sk);
        DrisClient client = DrisClient.newBuilder()
            .withCredential(auth)
            .withRegion(DrisRegion.valueOf("cn-north-4"))
            .build();

        try {
            String rsuId = createRsu(client);

            listRsus(client);

            updateRsu(client, rsuId);

            deleteRsu(client, rsuId);
        } catch (ConnectionException e) {
            logger.error("connection error", e);
        } catch (RequestTimeoutException e) {
            logger.error("RequestTimeoutException", e);
        } catch (ServiceResponseException e) {
            logger.error("ServiceResponseException", e);
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }

    private static void deleteRsu(DrisClient client, String rsuId) {
        DeleteRsuRequest request = new DeleteRsuRequest();
        request.setRsuId(rsuId);
        DeleteRsuResponse response = client.deleteRsu(request);
        System.out.println(response.toString());
    }

    private static void updateRsu(DrisClient client, String rsuId) {
        UpdateRsuRequest request = new UpdateRsuRequest();
        request.setRsuId(rsuId);
        ModifyRsuRequestDTO body = new ModifyRsuRequestDTO();
        body.setDescription("update rsu");
        request.withBody(body);
        UpdateRsuResponse response = client.updateRsu(request);
        System.out.println(response.toString());
    }

    private static void listRsus(DrisClient client) {
        BatchShowRsusRequest request = new BatchShowRsusRequest();
        BatchShowRsusResponse response = client.batchShowRsus(request);
        System.out.println(response.toString());
    }

    private static String createRsu(DrisClient client) {
        CreateRsuRequest request = new CreateRsuRequest();
        AddRsuDTO body = new AddRsuDTO();
        body.setName("<YOUR RSU NAME>");
        body.setEsn("<YOUR RSU ESN>");

        // 设置RSU相关联的Edge设备个数，如RSU仅关联1个Edge
        body.setRelatedEdgeNum(1);

        request.withBody(body);
        CreateRsuResponse response = client.createRsu(request);
        System.out.println(response.toString());

        return response.getRsuId();
    }
}
